package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 韶关市委宣传部<br>
 * http://fj.wenming.cn/ 中国文明网福建站
 * http://xm.wenming.cn/ 厦门市文明办
 * @author KK
 *
 */
public class WenmingNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://sg.wenming.cn/[a-z_]*/[a-z_]*/[0-9]*/t[0-9]{8}_[0-9]*.html",
		"http://sg.wenming.cn/[a-z_]*/[0-9]*/t[0-9]{8}_[0-9]*.html",
		"http://fj.wenming.cn/.*/[0-9]{6}/t[0-9]{8}_[0-9]*.htm",
		"http://xm.wenming.cn/.*/[0-9]{6}/t[0-9]{8}_[0-9]*.htm",
		"http://jx.wenming.cn/[a-z/]*/[0-9]*/t[0-9]{8}_[0-9]*.html",
		"http://zjsx.wenming.cn/[a-z/]*/[0-9]*/t[0-9]{8}_[0-9]*.html",
		"http://jdz.wenming.cn/[a-z/]*/[0-9]*/t[0-9]{8}_[0-9]*.html",
//		"http://zjsx.wenming.cn/[a-z]*/",
//		"http://[a-z]*.wenming.cn/[a-z]*/[a-z]*/",
//		"http://[a-z]*.wenming.cn/[a-z]*/",
//		"http://jx.wenming.cn/[a-z]+",
		"http://jx.wenming.cn/hezuo/201509/t[0-9]*_[0-9]*.htm",
//		"http://www.wenming.cn/[a-z]*/[a-z]*_[a-z]*/",
		"http://www.wenming.cn/wmpl_pd/yczl/[0-9]*/t[0-9]*_[0-9]*.shtml",
		"http://www.wenming.cn/sbhr_pd/hr365/[a-z]*/[0-9]*/t[0-9]*_[0-9]*.shtml",
		"http://xm.wenming.cn/zt/[0-9]*/t[0-9]*_[0-9]*_[0-9]*.htm",
		"http://fj.wenming.cn/[a-z]*/[0-9]*/t[0-9]*_[0-9]*.html",
		"http://www.wenming.cn/[a-z]*_294/[a-z]*/[a-z]*/",
		"http://gzas.wenming.cn/.*/[0-9]+/t[0-9\\_]+.html",
		"http://[a-z]+.wenming.cn/[a-z0-9\\/]+/t[0-9\\_]+.htm"

	};	
	
	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}		
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
						
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if (url.startsWith("http://fj.wenming.cn/")) {
			title =doc.select("html body div.container div.kk-3 div.font-3").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("html body div.container div.kk-3 div div.TRS_Editor"));			
			date=DateUtils.matchDate(doc.select("html body div.container div.kk-3 div.bg-2").text());
		}else if (url.startsWith("http://xm.wenming.cn/")) {
			title =doc.select("html body div#wrapper.cbody980 div#container.left div#content h4.th4").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("html body div#wrapper.cbody980 div#container.left div#content div.content"));			
			date=DateUtils.matchDate(doc.select("html body div#wrapper.cbody980 div#container.left div#content h5.th5").text());
			
		}else if (url.startsWith("http://jx.wenming.cn/")) {
			title =doc.select("div.maintitle").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#content"));			
			date=DateUtils.matchDate(doc.select("div.createdate").text());
			
		}else if (url.startsWith("http://zjsx.wenming.cn/")) {
			title =doc.select("td.title.bold").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("td#tex"));			
			date=DateUtils.matchDate(url);
			
		}else if (url.startsWith("http://jdz.wenming.cn/")) {
			title =doc.select("td.main18 strong").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
			date=DateUtils.matchDate(doc.select("td[width=36%]").text());
			
		}
		else if (url.startsWith("http://gzas.wenming.cn/")){
			title =doc.select("div.zwmain h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.Custom_UnionStyle"));			
			date=DateUtils.matchDate(doc.select("div.zwfrom").text());
			
		}
		else if (url.startsWith("http://dq.wenming.cn/")){
			title =doc.select("td.fyk").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.Custom_UnionStyle"));			
			date=DateUtils.matchDate(doc.select("td[style=font-size:12px;]").text());
		}
		if (StringUtils.isNullOrEmpty(title)){
		}
		if (StringUtils.isNotBlank(title) && date!=null){			
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);	
			news.setType(1);	
		}
		return news;
	}
	
	public static void main(String[] args) {
		WenmingNewsAnalyse analyse=new WenmingNewsAnalyse();
		String url="http://dq.wenming.cn/zyfw/201511/t20151105_2102454.htm";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
